// 获取当前账户下的商品
$(document).ready(function () {
    // 接收页面传递得参数id
    let id = location.search.split("=")[1];
    $.ajax({
        url: "http://jx.xuzhixiang.top/ap/api/cart-list.php",
        type: "get",
        data: {
            id: 74806,
        },
        success: function (res) {
            console.log(res);
            let str = "";
            res.data.forEach(item => {
                str += `
            <div class="cart1" >
                <div class="p-checkbox">
                    <input type="checkbox" name="" id="" checked="" class="j-checkbox">
                </div>
                <div class="p-goods">
                    <div class="p-img">
                        <img  alt="" class="zp" src="${item.pimg}">
                    </div>
                    <div class="p-msg sp">${item.pname}</div>
                </div>
                <div class="p-price">￥${item.pprice}</div>
                <div class="p-num">
                    <div class="quantity-form">
                        <p href="" class="decrement">-</p>
                        <input type="text" class="itxt"  value="${item.pnum}" data-id="${item.pid}">
                        <p href="" class="increment">+</p>
                    </div>
                </div>
                <div class="p-sum">￥12.60</div>
                <button data-id="${item.pid}">删除</button>
           </div>
       `;
            });
            $(".cart-item-list").html(str);
            //传递商品
            //加
            let oMinus = document.querySelector(".decrement");//-
            let oPlus = document.querySelector(".increment");//+
            let oNum = document.querySelector(".itxt");//he
            // let pid = $(".itxt").attr(str);
            oMinus.onclick = function () {
                oNum.value--;
                if (oNum.value < 1) {
                    oNum.value = 1;
                }
            }
            oPlus.onclick = function () {
                oNum.value++;
            };
            oNum.onchange = function () {
                
                // let id=$(this).data("id");
                if (oNum.value < 1) {
                    oNum.value = 1;
                    console.log(oNum.value);
                }
            };
            $.ajax({
                url:"http://jx.xuzhixiang.top/ap/api/cart-update-num.php",
                type:"get",
               data:{
                uid:74806,
                pnum:oNum.value,
                pid:id,

               },
               success:function(res){
                   console.log(res);
               }

            })
            //减


            // 删除
            // console.log(pid);
            $("button").click(function () {
                //页面移除当前得商品
                $(this).parent().remove();
                //获取当前id
                let id=$(this).data("id");
                console.log(id);
                $.ajax({
                    url: "http://jx.xuzhixiang.top/ap/api/cart-delete.php",
                    type: "get",
                    data: {
                        uid: 74806,
                        pid:id,
                    },
                  
                    success: function (res) {
                            console.log(res);


                    }
                })
            })
            //删除结束
// bu

//bu



        },

    })



})